﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data; 

namespace websitev2.Core
{
    public class RecentActivity
    {

        

        public static Models.RecentActivityModel[] ReturnRecentActivity( int RowsToReturn)
        {
            
            // Setup the recent activity list. 
            Models.RecentActivityModel[] oActivity = new Models.RecentActivityModel[RowsToReturn];
            int CurrentRow = 0;

            int _UserActivityToReturn = RowsToReturn / 2;
            int _TrophiesToReturn = RowsToReturn / 2; 

            // We should return a mix and match of different things

            

            // User Login
            #region Recent User Logins
            DataTable oLoginTable = websitev2.Core.SqlAccessCore.ReturnDataTableNewDB("select top " + _UserActivityToReturn.ToString() + " * from aspnet_Users where ApplicationID = 'F0768D3B-EF5B-4899-96FB-FA0A2B175CAE' order by LastActivityDate desc ");
            foreach (DataRow oRow in oLoginTable.Rows)
            {
                Models.RecentActivityModel oActive = new Models.RecentActivityModel();
                oActive.Activity = "Logged in";
                oActive.ActivityDate = (DateTime)oRow["LastActivityDate"];
                oActive.ActivityType = 1; 
                oActive.AssociatedImageURL = UserCore.ReturnAvatarURL(new Guid(oRow["userid"].ToString()),2); 
                oActive.UserName = (string)oRow["UserName"];
                oActivity[CurrentRow] = oActive;
                CurrentRow = CurrentRow + 2; 
            }
            #endregion 

            // Odd numbered rows
            CurrentRow = 1;

            DataTable oTrophyTable = StartStop.Helpers.Sql.ReturnDataTable(@" 
SELECT TOP " + _TrophiesToReturn.ToString() + @"
	tl.* , su.UserName, at.TName
	FROM [StartStop].[dbo].[TrophiesUserLink] tl 
  inner join ssUsers su on tl.UserID = su.UserID
  inner join Trophies at on tl.TrophyID = at.TrophyID
  where tl.AwardedYet =1 
  order by tl.AwardedOn desc

");
            foreach (DataRow oRow in oTrophyTable.Rows)
            {
                Models.RecentActivityModel oActive = new Models.RecentActivityModel();
                oActive.Activity = oRow["TNAME"].ToString() +" Awarded";
                oActive.ActivityDate = (DateTime)oRow["AwardedOn"];
                // Check for an image
                oActive.LinkURL = "/award/trophy/" + oRow["trophyID"].ToString();
                oActive.ActivityType = 2; 
                oActive.AssociatedImageURL = "/images/Trophysmall.png";
                oActive.UserName = (string)oRow["UserName"];
                oActivity[CurrentRow] = oActive;
                CurrentRow = CurrentRow + 2;
            }


            return oActivity; 
        }

    }
}